<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-06-16 10:57:19
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-12 16:23:19
-->
<template>
  <div>
    <div style="height: 70vh; overflow: auto">
      <el-tree
        :data="data"
        show-checkbox
        ref="tree"
        node-key="id"
        accordion
        :default-checked-keys="checkedKeys"
      >
      </el-tree>
    </div>
    <div>
      <el-button
        type="primary"
        size="default"
        @click="onSubmit"
        style="margin-top: 20px; margin-bottom: 50px"
      >
        确定
      </el-button>
    </div>
  </div>
</template>

<script>
import { roleMenuTreeselect, putRoleAll } from "@/api/sys";
export default {
  props: {
    roleId: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      checkedKeys: [],
      data: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onSubmit() {
      putRoleAll({
        roleId: this.roleId,
        menuIds: this.getMenuAllCheckedKeys(),
      }).then((res) => {
        this.$message.success(res.msg);
      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.tree.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    getList() {
      roleMenuTreeselect({ roleId: this.roleId }).then((res) => {
        this.checkedKeys = res.checkedKeys;
        this.data = res.menus;
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-tree-node__label {
  font-size: 16px !important;
}
/deep/ .el-tree-node {
  margin-top: 15px;
}
</style>